package messaging;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.naming.NamingException;

import extractor.StatusData;

public class MessageTest {

	public static void main(String[] args) throws JMSException {
		
		String factoryName = "ConnectionFactory";
		String destinationName = "topic1";

		// create subscriberGateway and publisherGateway
		SubscriberGateway subscriberGateway;
		PublisherGateway publisherGateway;
		try {
			// create a connection and a session to subscribe data
			subscriberGateway = new SubscriberGateway(factoryName,
					destinationName);
			// create a connection and a session  to publish data
			publisherGateway = new PublisherGateway(factoryName,
					destinationName);

			// register a message Listener to the session
			subscriberGateway.registerSubscriber(new MessageListener() {
				@Override
				public void onMessage(Message msg) {
					ObjectMessage message = (ObjectMessage) msg;
					String data;
					try {
						data = (String) ((ObjectMessage) message).getObject();
						System.out.println("msg = " + data + ".");
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});

			// publish message to the session
			for (int i = 0; i < 100; i++) {
				String msg = "message " + i;
				;
				publisherGateway.publish(msg);
			}

		} catch (NamingException | JMSException e) {
			e.printStackTrace();
		}
	}

}
